package org.net.device.chart;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import javax.sql.DataSource;

import org.dolphin.base.data.dao.SimpleDAO;
import org.dolphin.base.data.provider.JDBCFactory;
import org.jfree.data.time.Minute;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.net.device.HostStorageData;
import org.net.device.dao.hostresourcedao;
import org.net.device.info.hostresourceinfo;
import org.net.device.HostStorageData;
import org.net.device.HostStorageDataResult;

public class hostdataview extends dataview {
	hostresourceinfo hrinfo = null;

	public hostdataview(DataSource ds) {
		super(ds);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void getDatas() {
		// TODO Auto-generated method stub
		SimpleDAO sd = new SimpleDAO(this.getDs());

		sd.setClvo(HostStorageData.class);
		sd.setTable_name("hoststoragedatas");
		//select hostid,ifindex,times,hrAllocation,hrFail,hrSize,hrUsed from hoststoragedatas
		String[] fields = {"times","hrAllocation","hrFail","hrSize","hrUsed"};
		sd.setFields(fields);
		sd.setOrderfields("times");
		if (this.getTodate().getTime() - this.getFromdate().getTime() == 0) {
			this.setTimeaxis(this.getTodate().getTime(), 0, 0);
		}
		sd.setConditions(" hostid="+this.getHostid()+" and ifindex="+this.getIfindex()+" and times >= "+getFromdate().getTime()+" and times <= "+getTodate().getTime());
		ArrayList al = null;
		try {
			al = (ArrayList)sd.getalldata();
			this.setWatchDatas(al);
		}
		catch (Exception e) {
			
			System.out.println(e.getMessage());
		}        

	}

	@Override
	public void LoadResourceinfo(int id, int index) {
		// TODO Auto-generated method stub
		hostresourcedao sd = new hostresourcedao(this.getDs());
		sd.setConditions(" hostid = "+id+" and hrindex = "+index);
		hrinfo = sd.getHostResourceinfo();
	}
	

	public XYDataset createDataSet(String seriesname,int Graphtype) {
		getDatas();
		HostStorageData hrSData= null;
		TimeSeries s1 = new TimeSeries(seriesname,Minute.class);
		try {		
			for (int i = 0;i < getWatchDatas().size();i++) {
				hrSData = (HostStorageData)getWatchDatas().get(i);
				Date d = new Date();
				d.setTime(hrSData.getTimes());
				s1.addOrUpdate(new Minute(d),HostStorageDataResult.getUsUtilization(hrSData.getHrUsed(),hrSData.getHrSize()));
				//System.out.println(new Minute(d)+"  :   "+HostStorageDataResult.getUsUtilization(hrSData.getHrUsed(),hrSData.getHrSize()));
			}

		}
		catch (Exception e) {
			
			System.out.println(e.getMessage());
		}
		TimeSeriesCollection dataset = new TimeSeriesCollection();
		dataset.addSeries(s1);
		return dataset;

	}

}
